package com.kate.mall.member.config;

import cn.hutool.core.util.StrUtil;
import com.kate.mall.log.constants.TraceConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: FeignL拦截器解决请求头丢失的问题(traceId丢失问题)
 * @Auther: 新梦想*陈超
 * @Date: 2022/1/27 17:43
 */
@Configuration
public class FeignInterceptorConfig {
    @Bean
    public RequestInterceptor requestInterceptor(){
        RequestInterceptor requestInterceptor = new RequestInterceptor() {

            @Override
            public void apply(RequestTemplate template) {
                //传递traceId
                String traceId = StrUtil.isNotEmpty(MDC.get(TraceConstant.LOG_TRACE_ID))  ?
                        MDC.get(TraceConstant.LOG_TRACE_ID) :  MDC.get(TraceConstant.LOG_B3_TRACEID) ;
                if (StrUtil.isNotEmpty(traceId)) {
                    template.header(TraceConstant.HTTP_HEADER_TRACE_ID, traceId);
                }

            }
        };
        return requestInterceptor;
    }
}